package com.ltf.controller;

import com.ltf.entity.XJson;
import com.ltf.entity.XUser;
import com.ltf.service.LargeFileService;
import com.ltf.service.XUserService;
import com.ltf.utils.PackJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.*;

@RestController//@ResponseBody+@Controller 可以省去下面每个方法都加ResponseBody
@RequestMapping("/api/user")
public class XUserController {
    //打印输出换成这个
    private Logger logger=LoggerFactory.getLogger(this.getClass());
    @Autowired
    XUserService userService;
    @Autowired
    LargeFileService largeFileService;
    @Autowired
    PasswordEncoder passwordEncoder;

    //获取用户信息
    @GetMapping("/info")
    public XJson getUserInfo(){
        //当前登录用户的账号
        Object phone = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        logger.debug("当前登录账号="+phone);
        if (phone==null){
            return PackJson.packX(401,"未登录","");
        }else {
            //查询用户
            XUser xUser = this.userService.findByPhone((String) phone);
            logger.debug("查询到的用户="+xUser);
            if (xUser==null){
                return PackJson.packX(401,"用户不存在","");
            }else {
                //将当前登录的用户信息获取出来给到前端
                Map<String,Object>map=new HashMap<>();
                map.put("uname",xUser.getUname());
                map.put("phone",xUser.getPhone());
                map.put("role",xUser.getRole());
                map.put("pic",xUser.getPic());
                return PackJson.packX(200,"用户信息",map);
            }
        }
    }

    //修改头像
    @PostMapping("/updatePic")
    public XJson updatePic(@RequestParam(value = "mypic",required = false)MultipartFile myfile)throws IOException {
        if (myfile==null){
            return PackJson.packX(401,"图片上传为空","");
        }else {
            //不为空就去改当前登录用户的头像,先查看当前登录的账号是否过期，然后再查询出来修改
            Object phone = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            logger.debug("登录的用户="+phone);
            if (phone==null){
                return PackJson.packX(401,"当前未登录","");
            }else {
                XUser user = this.userService.findByPhone((String) phone);
                if (user!=null){
                    String uuid = UUID.randomUUID().toString();
                    uuid = uuid.replace("-", "");
                    user.setPic(uuid);
                    this.largeFileService.add(uuid, myfile.getOriginalFilename(), myfile.getBytes());
                    this.userService.update(user);
                    return PackJson.packX(200, "更新头像成功", uuid);
                }
                return PackJson.packX(402,"用户不存在","");
            }
        }
    }
    //修改密码
    @PostMapping("/updatePwd")
    public XJson updatePwd(String oldPwd, String newPwd){
        Object phone = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            XUser newUser = this.userService.findByPhone((String) phone);
            if (newUser == null) {
                return PackJson.packX(401, "该用户不存在", "");
            } else if (!this.passwordEncoder.matches(oldPwd, newUser.getPwd())) {
                return PackJson.packX(401, "旧密码输入有误", "");
            } else {
                newUser.setPic((String)null);
                newUser.setUname((String)null);
                newUser.setPhone((String)null);
                newUser.setRole((String)null);
                newUser.setPwd(newPwd);
                int x = this.userService.updatePwd(newUser);
                return x == 0 ? PackJson.packX(401, "密码修改失败", "") : PackJson.packX(200, "密码成功修改", "");
            }
        }
}
